package com.example.toolbar.net;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;

import com.ab.http.AbHttpListener;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.model.AbResult;
import com.ab.util.AbFileUtil;
import com.ab.util.AbJsonUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.toolbar.module.Article;
import com.example.toolbar.utils.imgUtils;

public class NetworkWeb {

	private AbHttpUtil mAbHttpUtil = null;
	private Context mContext = null;

	public NetworkWeb(Context context) {
		// 创建Http工具类
		mContext = context;
		mAbHttpUtil = AbHttpUtil.getInstance(context);
	}

	/**
	 * Create a new instance of SettingWeb.
	 */
	public static NetworkWeb newInstance(Context context) {
		NetworkWeb web = new NetworkWeb(context);
		return web;
	}

	/**
	 * 调用请求的模版
	 * 
	 * @param param1
	 *            参数1
	 * @param param2
	 *            参数2
	 * @param abHttpListener
	 *            请求的监听器
	 */
	public void testHttpGet(String param1, String param2,
			final AbHttpListener abHttpListener) {

		// 一个url地址
		String urlString = "http://www.amsoft.cn/rss.php";
		mAbHttpUtil.get(urlString, new AbStringHttpResponseListener() {

			@Override
			public void onSuccess(int statusCode, String content) {
				// 将结果传递回去
				abHttpListener.onSuccess(content);
			}

			@Override
			public void onStart() {

			}

			@Override
			public void onFinish() {

			}

			@Override
			public void onFailure(int statusCode, String content,
					Throwable error) {
				// 将失败错误信息传递回去
				abHttpListener.onFailure(content);
			}

		});
	}

	/**
	 * 调用一个列表请求
	 * 
	 * @param AbRequestParams
	 *            参数列表
	 * @param abHttpListener
	 *            请求的监听器
	 */
	public void findLogList(AbRequestParams params,
			final AbHttpListener abHttpListener, String urlString) {

		// 一个url地址
		// String urlString = "http://japi.juhe.cn/joke/content/list.from?";
		mAbHttpUtil.get(urlString, params, new AbStringHttpResponseListener() {

			@Override
			public void onSuccess(int statusCode, String content) {
				try {
					// 模拟数据
					JSONObject object = JSON.parseObject(content);
					if ("true".equals(object.getString("success"))) {
						// 成功
						JSONArray data = object.getJSONArray("data");
						//ArrayList<Article> articles = parseData(data);
						// 将结果传递回去
						abHttpListener.onSuccess(content);
					} else {
						// 将错误信息传递回去
						abHttpListener.onFailure("未知的错误");
					}
				} catch (Exception e) {
					e.printStackTrace();
					abHttpListener.onFailure(e.getMessage());
				}
			}
/*
			private ArrayList<Article> parseData(JSONArray data) {
				ArrayList<Article> articles = new ArrayList<Article>();
				for (int i = 0; i < data.size(); i++) {
					JSONObject curResult = data.getJSONObject(i);
					String tilte = curResult.getString("title");
					String time = curResult.getString("dateline");
					String ims = curResult.getString("images");
					String[] urls = imgUtils.getImageUrl(ims);
					String commentNum = curResult
							.getString("commentNum");
					String source = curResult.getString("source");
					String id = curResult.getString("id");
					String facts = curResult.getString("fact");
					String views = curResult.getString("views");
					String good = curResult.getString("good");
					String bad = curResult.getString("bad");
					articles.add(new Article(id, tilte, urls, time,
							commentNum, facts, source, views, good,
							bad));
				}
				return articles;
			}
*/
			@Override
			public void onStart() {
				// 开始的状态传递回去
			}

			@Override
			public void onFinish() {
				// 完成的状态传递回去
			}

			@Override
			public void onFailure(int statusCode, String content,
					Throwable error) {
				// 将失败错误信息传递回去
				abHttpListener.onFailure(error.toString());
			}

		});

	}

}
